package at.jku.se.pr.quickshopper.model;

import java.math.BigDecimal;
import java.util.ArrayList;

public class ArticleModel {
	
	/**
	 * list of current picked articles
	 */
	private ArrayList<Article> mArtikelList;
	
	/**
	 * a temporary storage which is used for adding a new article
	 */
	private ArrayList<Article> mTmpList;

	/**
	 * constructor which creates a new list of articles
	 * and creates also a list for temporary storage
	 */
	public ArticleModel() {
		super();
		this.mArtikelList = new ArrayList<Article>();
		this.mTmpList = new ArrayList<Article>();
	}

	/**
	 * add/scan a new article
	 * update the ArrayList mArtikelList
	 * @param a
	 */
	public void addArticle(Article a) {
		if (mArtikelList.size() == 0) {
			mArtikelList.add(a);
		} else {
			mTmpList.clear();
			mTmpList.add(a);
			mTmpList.addAll(1, mArtikelList);
			mArtikelList.clear();
			mArtikelList.addAll(mTmpList);
		}
	}

	/**
	 * get the current list of scanned articles
	 * @return
	 */
	public ArrayList<Article> getList() {
		return mArtikelList;
	}

	/**
	 * remove all articles ("Liste leeren")
	 */
	public void clearList() {
		mArtikelList.clear();
	}

	/**
	 * calculates the overall price of all listed articles
	 * @return sum overall price
	 */
	public BigDecimal getSumPriceFromList() {
		BigDecimal sum = BigDecimal.valueOf(0.0d);
		for (Article a : mArtikelList) {
			BigDecimal amount = BigDecimal.valueOf(a.getmAmount());
			sum = sum.add(a.getmPrice().multiply(amount));
		}
		return sum;
	}

	/**
	 * calculates the overall amount of all listed articles
	 * @return sum total number of articles
	 */
	public int getSumAmountFromList() {
		int sum = 0;
		for (Article a : mArtikelList) {
			sum += a.getmAmount();
		}
		return sum;
	}

	/**
	 * method to change the amount of an listed article
	 * @param pos
	 * @param amount
	 */
	public void setAmountOfArticle(int pos, int amount) {
		Article a = mArtikelList.get(pos);
		a.setmAmount(amount);
	}
	
}
